/*
 * @(#)DmsCheckoutHistory.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.bean.DmsCheckoutHistory;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.web.ListDmsCheckOutDocumentForm;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  DmsCheckoutHistoryDAObject.java

  This class is the data access bean for table "DMS_CHECKOUT_HISTORY".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   17/08/2003
  @version     $Revision: 1.18.2.1 $
*/

public class DmsCheckoutHistoryDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.18.2.1 $";

  public static final String TABLE_NAME = "DMS_CHECKOUT_HISTORY";

  public DmsCheckoutHistoryDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("DOCUMENT_ID");
    this.vecDBColumn.add("VERSION_ID");
    this.vecDBColumn.add("CHECKOUT_TIME");
    this.vecDBColumn.add("CHECKIN_TIME");
    this.vecDBColumn.add("CHECKOUT_STATUS");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("CHECKOUT_PERSON_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_ID, A.CHECKOUT_TIME, A.CHECKIN_TIME, A.CHECKOUT_STATUS, A.DESCRIPTION, A.CHECKOUT_PERSON_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_CHECKOUT_HISTORY A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsCheckoutHistory tmpDmsCheckoutHistory = new DmsCheckoutHistory();
          tmpDmsCheckoutHistory.setID(getResultSetInteger(rs, "ID"));
          tmpDmsCheckoutHistory.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsCheckoutHistory.setVersionID(getResultSetInteger(rs, "VERSION_ID"));
          tmpDmsCheckoutHistory.setCheckoutTime(getResultSetTimestamp(rs, "CHECKOUT_TIME"));
          tmpDmsCheckoutHistory.setCheckinTime(getResultSetTimestamp(rs, "CHECKIN_TIME"));
          tmpDmsCheckoutHistory.setCheckoutStatus(getResultSetString(rs, "CHECKOUT_STATUS"));
          tmpDmsCheckoutHistory.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsCheckoutHistory.setCheckoutPersonID(getResultSetInteger(rs, "CHECKOUT_PERSON_ID"));
          tmpDmsCheckoutHistory.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsCheckoutHistory.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsCheckoutHistory.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsCheckoutHistory.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsCheckoutHistory.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsCheckoutHistory.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
          tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
          return(tmpDmsCheckoutHistory);
        } else {
          //throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_ID, A.CHECKOUT_TIME, A.CHECKIN_TIME, A.CHECKOUT_STATUS, A.DESCRIPTION, A.CHECKOUT_PERSON_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_CHECKOUT_HISTORY A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }*/
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsCheckoutHistory tmpDmsCheckoutHistory = new DmsCheckoutHistory();
          tmpDmsCheckoutHistory.setID(getResultSetInteger(rs, "ID"));
          tmpDmsCheckoutHistory.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsCheckoutHistory.setVersionID(getResultSetInteger(rs, "VERSION_ID"));
          tmpDmsCheckoutHistory.setCheckoutTime(getResultSetTimestamp(rs, "CHECKOUT_TIME"));
          tmpDmsCheckoutHistory.setCheckinTime(getResultSetTimestamp(rs, "CHECKIN_TIME"));
          tmpDmsCheckoutHistory.setCheckoutStatus(getResultSetString(rs, "CHECKOUT_STATUS"));
          tmpDmsCheckoutHistory.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsCheckoutHistory.setCheckoutPersonID(getResultSetInteger(rs, "CHECKOUT_PERSON_ID"));
          tmpDmsCheckoutHistory.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsCheckoutHistory.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsCheckoutHistory.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsCheckoutHistory.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsCheckoutHistory.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsCheckoutHistory.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
          tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
          tmpDmsCheckoutHistory.setRecordCount(totalNumOfRecord);
          tmpDmsCheckoutHistory.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsCheckoutHistory);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_ID, A.CHECKOUT_TIME, A.CHECKIN_TIME, A.CHECKOUT_STATUS, A.DESCRIPTION, A.CHECKOUT_PERSON_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_CHECKOUT_HISTORY A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsCheckoutHistory tmpDmsCheckoutHistory = new DmsCheckoutHistory();
          tmpDmsCheckoutHistory.setID(getResultSetInteger(rs, "ID"));
          tmpDmsCheckoutHistory.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsCheckoutHistory.setVersionID(getResultSetInteger(rs, "VERSION_ID"));
          tmpDmsCheckoutHistory.setCheckoutTime(getResultSetTimestamp(rs, "CHECKOUT_TIME"));
          tmpDmsCheckoutHistory.setCheckinTime(getResultSetTimestamp(rs, "CHECKIN_TIME"));
          tmpDmsCheckoutHistory.setCheckoutStatus(getResultSetString(rs, "CHECKOUT_STATUS"));
          tmpDmsCheckoutHistory.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsCheckoutHistory.setCheckoutPersonID(getResultSetInteger(rs, "CHECKOUT_PERSON_ID"));
          tmpDmsCheckoutHistory.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsCheckoutHistory.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsCheckoutHistory.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsCheckoutHistory.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsCheckoutHistory.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsCheckoutHistory.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
          tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
          result.add(tmpDmsCheckoutHistory);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsCheckoutHistory tmpDmsCheckoutHistory = (DmsCheckoutHistory)((DmsCheckoutHistory)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   DMS_CHECKOUT_HISTORY(ID, DOCUMENT_ID, VERSION_ID, CHECKOUT_TIME, CHECKIN_TIME, CHECKOUT_STATUS, DESCRIPTION, CHECKOUT_PERSON_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsCheckoutHistory.getDocumentID());
        setPrepareStatement(preStat, 3, tmpDmsCheckoutHistory.getVersionID());
        setPrepareStatement(preStat, 4, tmpDmsCheckoutHistory.getCheckoutTime());
        setPrepareStatement(preStat, 5, tmpDmsCheckoutHistory.getCheckinTime());
        setPrepareStatement(preStat, 6, tmpDmsCheckoutHistory.getCheckoutStatus());
        setPrepareStatement(preStat, 7, tmpDmsCheckoutHistory.getDescription());
        setPrepareStatement(preStat, 8, tmpDmsCheckoutHistory.getCheckoutPersonID());
        setPrepareStatement(preStat, 9, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 10, new Integer(0));
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        preStat.executeUpdate();
        tmpDmsCheckoutHistory.setID(nextID);
        tmpDmsCheckoutHistory.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsCheckoutHistory.setCreateDate(currTime);
        tmpDmsCheckoutHistory.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsCheckoutHistory.setUpdateDate(currTime);
        tmpDmsCheckoutHistory.setUpdateCount(new Integer(0));
        tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
        tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
        return(tmpDmsCheckoutHistory);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsCheckoutHistory tmpDmsCheckoutHistory = (DmsCheckoutHistory)((DmsCheckoutHistory)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_CHECKOUT_HISTORY ");
        sqlStat.append("SET  DOCUMENT_ID=?, VERSION_ID=?, CHECKOUT_TIME=?, CHECKIN_TIME=?, CHECKOUT_STATUS=?, DESCRIPTION=?, CHECKOUT_PERSON_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsCheckoutHistory.getDocumentID());
        setPrepareStatement(preStat, 2, tmpDmsCheckoutHistory.getVersionID());
        setPrepareStatement(preStat, 3, tmpDmsCheckoutHistory.getCheckoutTime());
        setPrepareStatement(preStat, 4, tmpDmsCheckoutHistory.getCheckinTime());
        setPrepareStatement(preStat, 5, tmpDmsCheckoutHistory.getCheckoutStatus());
        setPrepareStatement(preStat, 6, tmpDmsCheckoutHistory.getDescription());
        setPrepareStatement(preStat, 7, tmpDmsCheckoutHistory.getCheckoutPersonID());
        setPrepareStatement(preStat, 8, new Integer(tmpDmsCheckoutHistory.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 10, currTime);
        setPrepareStatement(preStat, 11, tmpDmsCheckoutHistory.getID());
        setPrepareStatement(preStat, 12, tmpDmsCheckoutHistory.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpDmsCheckoutHistory.setUpdaterID(sessionContainer.getUserRecordID());
          tmpDmsCheckoutHistory.setUpdateDate(currTime);
          tmpDmsCheckoutHistory.setUpdateCount(new Integer(tmpDmsCheckoutHistory.getUpdateCount().intValue() + 1));
          tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
          tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
          return(tmpDmsCheckoutHistory);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsCheckoutHistory tmpDmsCheckoutHistory = (DmsCheckoutHistory)((DmsCheckoutHistory)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_CHECKOUT_HISTORY ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsCheckoutHistory.getID());
        setPrepareStatement(preStat, 2, tmpDmsCheckoutHistory.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpDmsCheckoutHistory);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    DmsCheckoutHistory tmpDmsCheckoutHistory = (DmsCheckoutHistory)this.oldValue;

    if (tmpDmsCheckoutHistory != null) {
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getID()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getDocumentID()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getVersionID()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckoutTime()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckinTime()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckoutStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getDescription()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckoutPersonID()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getUpdateDate()));
    }

    tmpDmsCheckoutHistory = (DmsCheckoutHistory)obj;
    if (tmpDmsCheckoutHistory != null) {
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getID()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getDocumentID()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getVersionID()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckoutTime()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckinTime()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckoutStatus()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getDescription()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCheckoutPersonID()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpDmsCheckoutHistory.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/


/**
 * Gets list of checkout history by the version ID and the checkout status
 * 
 * @param versionID the version ID
 * @param checkoutStatus checkout status, value:
 * {@link com.dcivision.dms.bean.DmsCheckoutHistory#CURRENT_RECORD},{@link com.dcivision.dms.bean.DmsCheckoutHistory#HISTORY_RECORD}
 * @return list of DmsCheckoutHistory object
 * @throws ApplicationException
 * @see com.dcivision.bean.DmsCheckoutHistory
 */
  public synchronized List getObjectByVersionAndCheckoutStatus(Integer versionID, String checkoutStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_ID, A.CHECKOUT_TIME, A.CHECKIN_TIME, A.CHECKOUT_STATUS, A.DESCRIPTION, A.CHECKOUT_PERSON_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_CHECKOUT_HISTORY A ");
        sqlStat.append("WHERE  A.VERSION_ID = ? AND A.CHECKOUT_STATUS = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, versionID);
        this.setPrepareStatement(preStat, 2, checkoutStatus);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
        //log.debug("sql = " +sqlStat.toString());
        //log.debug("versionID = " +versionID + " checkoutStatus = " + checkoutStatus);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsCheckoutHistory tmpDmsCheckoutHistory = new DmsCheckoutHistory();
          tmpDmsCheckoutHistory.setID(getResultSetInteger(rs, "ID"));
          tmpDmsCheckoutHistory.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsCheckoutHistory.setVersionID(getResultSetInteger(rs, "VERSION_ID"));
          tmpDmsCheckoutHistory.setCheckoutTime(getResultSetTimestamp(rs, "CHECKOUT_TIME"));
          tmpDmsCheckoutHistory.setCheckinTime(getResultSetTimestamp(rs, "CHECKIN_TIME"));
          tmpDmsCheckoutHistory.setCheckoutStatus(getResultSetString(rs, "CHECKOUT_STATUS"));
          tmpDmsCheckoutHistory.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsCheckoutHistory.setCheckoutPersonID(getResultSetInteger(rs, "CHECKOUT_PERSON_ID"));
          tmpDmsCheckoutHistory.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsCheckoutHistory.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsCheckoutHistory.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsCheckoutHistory.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsCheckoutHistory.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsCheckoutHistory.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
          tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
          result.add(tmpDmsCheckoutHistory);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }


/**
 * Gets list of the checkout history by document ID
 * 
 * @param docID the document id
 * @return list of the DmsCheckoutHistory object
 * @throws ApplicationException
 * @see com.dcivision.bean.DmsCheckoutHistory
 */
  public synchronized List getListByDocumentID(Integer docID) throws ApplicationException {
  PreparedStatement preStat = null;
  ResultSet rs = null;
  StringBuffer sqlStat = new StringBuffer();
  List result = new ArrayList();

  synchronized(dbConn) {
    try {
      sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_ID, A.CHECKOUT_TIME, A.CHECKIN_TIME, A.CHECKOUT_STATUS, A.DESCRIPTION, A.CHECKOUT_PERSON_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
      sqlStat.append("FROM   DMS_CHECKOUT_HISTORY A ");
      sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.DOCUMENT_ID=? ");
      preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
      this.setPrepareStatement(preStat, 2, docID);
      rs = preStat.executeQuery();
      while (rs.next()) {
        DmsCheckoutHistory tmpDmsCheckoutHistory = new DmsCheckoutHistory();
        tmpDmsCheckoutHistory.setID(getResultSetInteger(rs, "ID"));
        tmpDmsCheckoutHistory.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
        tmpDmsCheckoutHistory.setVersionID(getResultSetInteger(rs, "VERSION_ID"));
        tmpDmsCheckoutHistory.setCheckoutTime(getResultSetTimestamp(rs, "CHECKOUT_TIME"));
        tmpDmsCheckoutHistory.setCheckinTime(getResultSetTimestamp(rs, "CHECKIN_TIME"));
        tmpDmsCheckoutHistory.setCheckoutStatus(getResultSetString(rs, "CHECKOUT_STATUS"));
        tmpDmsCheckoutHistory.setDescription(getResultSetString(rs, "DESCRIPTION"));
        tmpDmsCheckoutHistory.setCheckoutPersonID(getResultSetInteger(rs, "CHECKOUT_PERSON_ID"));
        tmpDmsCheckoutHistory.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
        tmpDmsCheckoutHistory.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
        tmpDmsCheckoutHistory.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
        tmpDmsCheckoutHistory.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
        tmpDmsCheckoutHistory.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
        tmpDmsCheckoutHistory.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
        tmpDmsCheckoutHistory.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getCreatorID()));
        tmpDmsCheckoutHistory.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsCheckoutHistory.getUpdaterID()));
        result.add(tmpDmsCheckoutHistory);
      }
      return(result);
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (SQLException sqle) {
      log.error(sqle, sqle);
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    } finally {
      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
      try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
    }
  }
}

  /**
   * Delete the record of table "DMS_CHECKOUT_HISTORY" by the document id.
   * 
   * @param dmsDocumentID the document id
   * @throws ApplicationException
   * 
   */
  public synchronized void deleteByDocID(Integer dmsDocumentID) throws ApplicationException {
	PreparedStatement preStat = null;
	StringBuffer sqlStat = new StringBuffer();


	synchronized(dbConn) {
      try {
	    int updateCnt = 0;
		sqlStat.append("DELETE ");
		sqlStat.append("FROM   DMS_CHECKOUT_HISTORY ");
		sqlStat.append("WHERE  DOCUMENT_ID=?  ");
		preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		setPrepareStatement(preStat, 1, dmsDocumentID);
		
		updateCnt = preStat.executeUpdate();
			
	   }catch (SQLException sqle) {
	     log.error(sqle, sqle);
		 throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	   } catch (Exception e) {
		 log.error(e, e);
		 throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
	   } finally {
			try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	   }
	}
  }
  
  public synchronized List getCheckoutList(AbstractSearchForm searchForm) throws ApplicationException {
  	PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsCheckOutDocumentForm checkoutForm=(ListDmsCheckOutDocumentForm)searchForm;

    synchronized(dbConn) {
      try {
      	sqlStat.append("SELECT A.ID, A.DOCUMENT_NAME, A.DOCUMENT_TYPE, A.PARENT_ID, A.ROOT_ID, A.PRIORITY, A.CREATE_TYPE, A.EFFECTIVE_START_DATE, A.EFFECTIVE_END_DATE, A.REMINDER_AMOUNT, A.REMINDER_TYPE, A.NOTIFICATION_WAY,A.OWNER_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.CHECKOUT_ID, A.ALERT_EDIT_ID, A.ALERT_INVITE_ID, A.USER_DEF_1, A.USER_DEF_2, A.USER_DEF_3, A.USER_DEF_4, A.USER_DEF_5, A.USER_DEF_6, A.USER_DEF_7, A.USER_DEF_8, A.USER_DEF_9, A.USER_DEF_10, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,B.CHECKOUT_TIME,B.CHECKIN_TIME ");
        sqlStat.append("FROM   DMS_DOCUMENT A ,DMS_CHECKOUT_HISTORY B ");
        if("B".equals(checkoutForm.getModeType())){
          sqlStat.append(",DMS_ROOT C ");
        }
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.CHECKOUT_ID=B.ID AND A.ITEM_STATUS=? ");
        if("A".equals(checkoutForm.getModeType())){
          sqlStat.append("AND B.CHECKOUT_PERSON_ID=? ");
        }else if("B".equals(checkoutForm.getModeType())){
          sqlStat.append("AND A.ROOT_ID=C.ID AND C.OWNER_ID=? AND B.CHECKOUT_PERSON_ID<>? ");
        }
        if(searchForm.getSortAttribute()!= null){
        	sqlStat.append(" order by A.");
        	sqlStat.append(searchForm.getSortAttribute() + " " + searchForm.getSortOrder());        	
        }
        	
        //Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, 2, DmsVersion.EXCLUSIVE_LOCK);
        this.setPrepareStatement(preStatCnt, 3, sessionContainer.getUserRecordID());
        if("B".equals(checkoutForm.getModeType())){
          this.setPrepareStatement(preStatCnt,4,sessionContainer.getUserRecordID());
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, DmsVersion.EXCLUSIVE_LOCK);
        this.setPrepareStatement(preStat, 3, sessionContainer.getUserRecordID());
        if("B".equals(checkoutForm.getModeType())){
          this.setPrepareStatement(preStat,4,sessionContainer.getUserRecordID());
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsDocument tmpDmsDocument = new DmsDocument();
          tmpDmsDocument.setID(getResultSetInteger(rs, "A.ID"));
          tmpDmsDocument.setDocumentName(getResultSetString(rs, "A.DOCUMENT_NAME"));
          tmpDmsDocument.setDocumentType(getResultSetString(rs, "A.DOCUMENT_TYPE"));
          tmpDmsDocument.setParentID(getResultSetInteger(rs, "A.PARENT_ID"));
          tmpDmsDocument.setRootID(getResultSetInteger(rs, "A.ROOT_ID"));
          tmpDmsDocument.setPriority(getResultSetString(rs, "A.PRIORITY"));
          tmpDmsDocument.setCreateType(getResultSetString(rs, "A.CREATE_TYPE"));
          tmpDmsDocument.setEffectiveStartDate(getResultSetTimestamp(rs, "A.EFFECTIVE_START_DATE"));
          tmpDmsDocument.setEffectiveEndDate(getResultSetTimestamp(rs, "A.EFFECTIVE_END_DATE"));
          tmpDmsDocument.setReminderAmount(getResultSetInteger(rs, "A.REMINDER_AMOUNT"));
          tmpDmsDocument.setReminderType(getResultSetString(rs, "A.REMINDER_TYPE"));
          tmpDmsDocument.setNotificationWay(getResultSetStringAry(rs, "A.NOTIFICATION_WAY", ";"));
          tmpDmsDocument.setOwnerID(getResultSetInteger(rs, "A.OWNER_ID"));
          tmpDmsDocument.setReferenceNo(getResultSetString(rs, "A.REFERENCE_NO"));
          tmpDmsDocument.setDescription(getResultSetString(rs, "A.DESCRIPTION"));
          tmpDmsDocument.setItemSize(getResultSetInteger(rs, "A.ITEM_SIZE"));
          tmpDmsDocument.setItemStatus(getResultSetString(rs, "A.ITEM_STATUS"));
          tmpDmsDocument.setCheckoutID(getResultSetInteger(rs, "A.CHECKOUT_ID"));
          tmpDmsDocument.setAlertEditID(getResultSetInteger(rs, "A.ALERT_EDIT_ID"));
          tmpDmsDocument.setAlertInviteID(getResultSetInteger(rs, "A.ALERT_INVITE_ID"));
          tmpDmsDocument.setUserDef1(getResultSetString(rs, "A.USER_DEF_1"));
          tmpDmsDocument.setUserDef2(getResultSetString(rs, "A.USER_DEF_2"));
          tmpDmsDocument.setUserDef3(getResultSetString(rs, "A.USER_DEF_3"));
          tmpDmsDocument.setUserDef4(getResultSetString(rs, "A.USER_DEF_4"));
          tmpDmsDocument.setUserDef5(getResultSetString(rs, "A.USER_DEF_5"));
          tmpDmsDocument.setUserDef6(getResultSetString(rs, "A.USER_DEF_6"));
          tmpDmsDocument.setUserDef7(getResultSetString(rs, "A.USER_DEF_7"));
          tmpDmsDocument.setUserDef8(getResultSetString(rs, "A.USER_DEF_8"));
          tmpDmsDocument.setUserDef9(getResultSetString(rs, "A.USER_DEF_9"));
          tmpDmsDocument.setUserDef10(getResultSetString(rs, "A.USER_DEF_10"));
          tmpDmsDocument.setRecordStatus(getResultSetString(rs, "A.RECORD_STATUS"));
          tmpDmsDocument.setUpdateCount(getResultSetInteger(rs, "A.UPDATE_COUNT"));
          tmpDmsDocument.setCreatorID(getResultSetInteger(rs, "A.CREATOR_ID"));
          tmpDmsDocument.setCreateDate(getResultSetTimestamp(rs, "A.CREATE_DATE"));
          tmpDmsDocument.setUpdaterID(getResultSetInteger(rs, "A.UPDATER_ID"));
          tmpDmsDocument.setUpdateDate(getResultSetTimestamp(rs, "A.UPDATE_DATE"));
          tmpDmsDocument.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocument.getCreatorID()));
          tmpDmsDocument.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocument.getUpdaterID()));
          tmpDmsDocument.setCheckoutDate(getResultSetTimestamp(rs,"B.CHECKOUT_TIME"));
          tmpDmsDocument.setRecordCount(totalNumOfRecord);
          tmpDmsDocument.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsDocument);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  //Add By Jim Zhou     2006/12/27    EIP-1276
  public synchronized List getCheckoutListSafety(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsCheckOutDocumentForm checkoutForm=(ListDmsCheckOutDocumentForm)searchForm;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_NAME, A.DOCUMENT_TYPE, A.PARENT_ID, A.ROOT_ID, A.PRIORITY, A.CREATE_TYPE, A.EFFECTIVE_START_DATE, A.EFFECTIVE_END_DATE, A.REMINDER_AMOUNT, A.REMINDER_TYPE, A.NOTIFICATION_WAY,A.OWNER_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.CHECKOUT_ID, A.ALERT_EDIT_ID, A.ALERT_INVITE_ID, A.USER_DEF_1, A.USER_DEF_2, A.USER_DEF_3, A.USER_DEF_4, A.USER_DEF_5, A.USER_DEF_6, A.USER_DEF_7, A.USER_DEF_8, A.USER_DEF_9, A.USER_DEF_10, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,B.CHECKOUT_TIME,B.CHECKIN_TIME ");
        sqlStat.append("FROM   DMS_DOCUMENT A ,DMS_CHECKOUT_HISTORY B ");
        if("B".equals(checkoutForm.getModeType())){
          sqlStat.append(",DMS_ROOT C ");
        }
        sqlStat.append("WHERE  A.DOCUMENT_TYPE <> 'L' AND A.RECORD_STATUS = ? AND A.CHECKOUT_ID=B.ID AND A.ITEM_STATUS=? ");
        if("A".equals(checkoutForm.getModeType())){
          sqlStat.append("AND B.CHECKOUT_PERSON_ID=? ");
        }else if("B".equals(checkoutForm.getModeType())){
          sqlStat.append("AND A.ROOT_ID=C.ID AND C.OWNER_ID=? AND B.CHECKOUT_PERSON_ID<>? ");
        }
          
        //Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, 2, DmsVersion.EXCLUSIVE_LOCK);
        this.setPrepareStatement(preStatCnt, 3, sessionContainer.getUserRecordID());
        if("B".equals(checkoutForm.getModeType())){
          this.setPrepareStatement(preStatCnt,4,sessionContainer.getUserRecordID());
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
        
        //Update By Jim Zhou      EIP-1591      2007/01/22
        if(searchForm.getSortAttribute()!= null){
          sqlStat.append(" order by A.");
          sqlStat.append(searchForm.getSortAttribute() + " " + searchForm.getSortOrder());          
        }
        //Update End

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, DmsVersion.EXCLUSIVE_LOCK);
        this.setPrepareStatement(preStat, 3, sessionContainer.getUserRecordID());
        if("B".equals(checkoutForm.getModeType())){
          this.setPrepareStatement(preStat,4,sessionContainer.getUserRecordID());
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsDocument tmpDmsDocument = new DmsDocument();
          tmpDmsDocument.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocument.setDocumentName(getResultSetString(rs, "DOCUMENT_NAME"));
          tmpDmsDocument.setDocumentType(getResultSetString(rs, "DOCUMENT_TYPE"));
          tmpDmsDocument.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsDocument.setRootID(getResultSetInteger(rs, "ROOT_ID"));
          tmpDmsDocument.setPriority(getResultSetString(rs, "PRIORITY"));
          tmpDmsDocument.setCreateType(getResultSetString(rs, "CREATE_TYPE"));
          tmpDmsDocument.setEffectiveStartDate(getResultSetTimestamp(rs, "EFFECTIVE_START_DATE"));
          tmpDmsDocument.setEffectiveEndDate(getResultSetTimestamp(rs, "EFFECTIVE_END_DATE"));
          tmpDmsDocument.setReminderAmount(getResultSetInteger(rs, "REMINDER_AMOUNT"));
          tmpDmsDocument.setReminderType(getResultSetString(rs, "REMINDER_TYPE"));
          tmpDmsDocument.setNotificationWay(getResultSetStringAry(rs, "NOTIFICATION_WAY", ";"));
          tmpDmsDocument.setOwnerID(getResultSetInteger(rs, "OWNER_ID"));
          tmpDmsDocument.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsDocument.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsDocument.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsDocument.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsDocument.setCheckoutID(getResultSetInteger(rs, "CHECKOUT_ID"));
          tmpDmsDocument.setAlertEditID(getResultSetInteger(rs, "ALERT_EDIT_ID"));
          tmpDmsDocument.setAlertInviteID(getResultSetInteger(rs, "ALERT_INVITE_ID"));
          tmpDmsDocument.setUserDef1(getResultSetString(rs, "USER_DEF_1"));
          tmpDmsDocument.setUserDef2(getResultSetString(rs, "USER_DEF_2"));
          tmpDmsDocument.setUserDef3(getResultSetString(rs, "USER_DEF_3"));
          tmpDmsDocument.setUserDef4(getResultSetString(rs, "USER_DEF_4"));
          tmpDmsDocument.setUserDef5(getResultSetString(rs, "USER_DEF_5"));
          tmpDmsDocument.setUserDef6(getResultSetString(rs, "USER_DEF_6"));
          tmpDmsDocument.setUserDef7(getResultSetString(rs, "USER_DEF_7"));
          tmpDmsDocument.setUserDef8(getResultSetString(rs, "USER_DEF_8"));
          tmpDmsDocument.setUserDef9(getResultSetString(rs, "USER_DEF_9"));
          tmpDmsDocument.setUserDef10(getResultSetString(rs, "USER_DEF_10"));
          tmpDmsDocument.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocument.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocument.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocument.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocument.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocument.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDocument.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocument.getCreatorID()));
          tmpDmsDocument.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocument.getUpdaterID()));
          tmpDmsDocument.setCheckoutDate(getResultSetTimestamp(rs,"CHECKOUT_TIME"));
          tmpDmsDocument.setRecordCount(totalNumOfRecord);
          tmpDmsDocument.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsDocument);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  //Add End
}
